package com.infindo.iflower;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import com.infindo.common.Constant;
import com.infindo.iflower.util.AssetsUtil;

public class GridGallery extends Activity {

	Button galleryBtn;
	Button favoriteBtn;
	Button aboutusBtn;
	Activity mActivity;
	OnTouchListener listener;
	private GridView g;

	private String[] picNames;
	private static final int FAV_INTENT_CODE = 100;
	private static final int GALLERY_INTENT_CODE = 101;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.grid_gallery);
		mActivity = this;

		g = (GridView) findViewById(R.id.myGrid);
		initGallery();

		g.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long rowid) {
				Intent intent = new Intent(mActivity, PictureView.class);
				Bundle b = new Bundle();
				b.putInt("picIndex", Integer.parseInt(view.getTag()
								.toString()));
				b.putString(Constant.ClASS_METHOD, Constant.FILENAME_FROM_GALLERY);
				b.putStringArray("picNames", picNames); // filenames from sd
				intent.putExtras(b);
				startActivityForResult(intent, GALLERY_INTENT_CODE);
			}
		});

		galleryBtn = (Button) findViewById(R.id.gallerybtn);
		galleryBtn.setOnTouchListener(touchLisenter);
		favoriteBtn = (Button) findViewById(R.id.favoritebtn);
		favoriteBtn.setOnTouchListener(touchLisenter);
		aboutusBtn = (Button) findViewById(R.id.aboutus);
		aboutusBtn.setOnTouchListener(touchLisenter);

		favoriteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent t = new Intent(mActivity, GridFavorite.class);
				startActivity(t);
				finish();
			}
		});

		aboutusBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GridGallery.this, AboutUs.class);
				startActivity(intent);
				finish();
			}
		});
		
		galleryBtn.setBackgroundColor(Color.GRAY);

	}

	private void initGallery() {
		picNames = AssetsUtil.getPictureList(this);
		
		g.setAdapter(Constant.getImageAdapterInstanceDefault(this, this, picNames, ""));
		g.invalidateViews();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == FAV_INTENT_CODE) {
			if (resultCode == RESULT_OK) {
				favoriteBtn.performClick();
			}
		}
	}

	private OnTouchListener touchLisenter = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				v.setBackgroundColor(Color.parseColor("#BDBDBD"));
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				// v.setBackgroundResource(R.drawable.play);
				v.setBackgroundColor(Color.parseColor("#848484"));
			}

			return false;
		}

	};

}